package com.ojoin.trade.common.web.ldap;

import java.net.URI;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.ojoin.trade.common.utils.Config;


@Service
public class LdapSettingsServiceImpl  implements LdapSettingsService {
    private static final Logger LOG = LoggerFactory.getLogger(LdapSettingsServiceImpl.class);
    
    @Autowired
    private   LdapSettings ldapSettings;

    @Override
    public LdapSettings load() {
    	//TOOD:remove
    	URI ldap=URI.create("ldap://211.154.166.228:389");
    	
    	boolean enable=Boolean.valueOf(Config.getProperty("sys.ldap.enable"));
    	
    	ldapSettings.setEnabled(enable);
    	ldapSettings.setSystemUsername("dc=18join,dc=com");
        ldapSettings.setSystemPassword("Ojoin923329");
        ldapSettings.setUri(ldap);
        ldapSettings.setUseStartTls(false);
        ldapSettings.setTrustAllCertificates(true);
        ldapSettings.setActiveDirectory(false);
        ldapSettings.setSearchBase("ou=User,dc=18join,dc=com");
        ldapSettings.setSearchPattern("(&(objectClass=inetOrgPerson)(uid={0}))");
        ldapSettings.setDisplayNameAttribute("displayName");
        
        Map groupMapping=Maps.newHashMap();
        groupMapping.put("dev","sys_admin");
        groupMapping.put("szqh_sl_ywzy","szqh_sl_ywzy");
        
        ldapSettings.setDefaultGroup("szqh_sl_ywzy");
        ldapSettings.setGroupMapping(groupMapping);
        ldapSettings.setGroupSearchBase("ou=Group,dc=18join,dc=com");
        ldapSettings.setGroupIdAttribute("cn");
        ldapSettings.setGroupSearchPattern("(objectClass=posixGroup)");
        ldapSettings.setAdditionalDefaultGroups(new HashSet());
    	
        return ldapSettings;
    }

    @Override
    public void delete() {
    }

	@Override
	public void save(LdapSettings ldapSettings) {
		
	}
}
